package com.ruoyi.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.ruoyi.common.result.Result;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.*;
import com.ruoyi.mapper.T24Mapper;
import com.ruoyi.service.IT03Service;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.T06Mapper;
import com.ruoyi.service.IT06Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 授权中心Service业务层处理
 *
 * @author w
 * @date 2024-07-25
 */
@Service
public class T06ServiceImpl implements IT06Service
{
    @Autowired
    private T06Mapper t06Mapper;
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private T24Mapper t24Mapper;
    @Autowired
    private IT03Service t03Service;

    /**
     * 查询授权中心
     *
     * @param F0601 授权中心主键
     * @return 授权中心
     */
    @Override
    public T06 selectT06ByF0601(Long F0601)
    {
        return t06Mapper.selectT06ByF0601(F0601);
    }
    /**
     * 查询授权中心列表
     *
     * @param
     * @return 授权中心
     */
    @Override
    public List<T06> selectT06List(T06 t06)
    {
        PageUtils.startPage();
        t06.setF0612(0L);
        return t06Mapper.selectT06List(t06);
    }

    /**
     * 新增授权中心
     *
     * @param t06 授权中心
     * @return 结果
     */
    @Override
    public int insertT06(T06 t06)
    {
        return t06Mapper.insertT06(t06);
    }

    /**
     * 修改授权中心
     *
     * @param t06 授权中心
     * @return 结果
     */
    @Override
    public int updateT06(T06 t06)
    {
        return t06Mapper.updateT06(t06);
    }

    /**
     * 批量删除授权中心
     *
     * @param F0601s 需要删除的授权中心主键
     * @return 结果
     */
    @Override
    public int deleteT06ByF0601s(Long[] F0601s)
    {
        return t06Mapper.deleteT06ByF0601s(F0601s);
    }

    /**
     * 删除授权中心信息
     *
     * @param F0601 授权中心主键
     * @return 结果
     */
    @Override
    public int deleteT06ByF0601(Long F0601)
    {
        sysUserService.deleteUserById(F0601);
        return t06Mapper.deleteT06ByF0601(F0601);
    }

    @Override
    public List<T06> selectT06ListByF0601s(List<Long> f0601s) {
        return t06Mapper.selectT06ListByF0601s(f0601s);
    }

    //查询人员身份类别
    @Override
    public List<String> selectF0609() {
        return t06Mapper.selectF0609();
    }

    @Override
    public Boolean checkUserNameUnique(String username) {
        T06  t06=t06Mapper.checkUserNameUnique(username);
        if (t06!=null){
            return false;
        }
        return true;
    }

    @Override
    public T06 selectByusername(String username) {
        return t06Mapper.selectByusername(username);
    }

    @Override
    public int updateById(T06small t06small, Long F0601) {
        return t06Mapper.updateById(t06small,F0601);
    }

    @Override
    public T06 selectByDeptId(Long f0501) {
        return t06Mapper.selectByDeptId(f0501);
    }

    @Override
    public List<T06> selectByF0301(Long f0301) {
        return t06Mapper.selectByF0301(f0301);
    }

    @Override
    public List<T06> selectNoPerssion(T06 t06,Long f0301) {
        return t06Mapper.selectNoPerssion(t06,f0301);
    }

    @Override
    public T06 selectByOpneid(String openId) {
        return t06Mapper.selectByOpneid(openId);
    }

    @Override
    public List<T06> selectNeedProcess(T06 t06) {
        return t06Mapper.selectNeedProcess(t06);
    }

    @Override
    public void updateProcess(Long f0601, Long l) {
         t06Mapper.updateProcess(f0601,l);
    }

    @Override
    @Transactional
    public int addActivity(T06 t06) {
        String id = UUID.randomUUID().toString();
        if(StringUtils.isNotEmpty(t06.getF0614())){
            id = t06.getF0614();
        }
        T24 t24=new T24();
        t24.setF1801(t06.getF0601());
        t24.setF2403(0L);
        if(StringUtils.isNotEmpty(t06.getF0602())){
           t24.setF2402(t06.getF0602());
        }
        t24.setF2406(new Date());
        t24.setF2405(id);
        t06.setF0614(id);
        t24.setF2404("审批提交");
        t24.setF2407(0L);
        t06.setF0612(2L);
        int i = t24Mapper.insertT24(t24);
        int s=t06Mapper.updateT06(t06);
        return s;
    }

    @Override
    public T06 getUserByF0614(String f0614) {
        return t06Mapper.getUserByF0614(f0614);
    }

    @Override
    @Transactional
    public Result sendTokens(List<Long> f0601s, Long number) {
        if(f0601s.size()<=0){
            return Result.erro("请选择人员");
        }
        Long F0601 = null;
        try {
            Object o = f0601s.get(0);
            Integer i = (Integer) o;
            F0601 = Long.valueOf(i);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        T06 t06 = t06Mapper.getT06ByF0601(F0601);
        T03 t03 = t03Service.selectT03ByF0301(t06.getF0301());
        if(t03.getF0310()<number*f0601s.size()){
            return Result.erro("发放数量大于公司剩余数量");
        }else{
            t03.setF0310(t03.getF0310()-number*f0601s.size());
        }
        t03Service.updateT03(t03);
        int x = t06Mapper.sendTokens(f0601s,number);
        return Result.success(x,"分配成功");
    }

    @Override
    public int deleteT06ByF0301(Long f0301) {
        return t06Mapper.deleteT06ByF0301(f0301);
    }

}
